<?php
class PersonAction extends BaseAction {
	protected  function _initialize(){
		parent::_initialize();
	}

	/**
	 * 默认动作显示人物属性
	 *
	 */
	public function index(){
		$cid = Session::get('cid');
		$char = D('Charview');
		$char->find("charbase.cid='$cid' and charplus.cid=charbase.cid");
		$this->assign('char',$char);
		//$char->zhuangbei1 = unserialize($char->zhuangbei1);
		$nowlevel = $char->level;
		$nextlevel = $nowlevel + 1;
		$needjingyan = 60*($nextlevel * $nextlevel * $nextlevel + 5 * $nextlevel) - 60 - (60*($nowlevel * $nowlevel * $nowlevel + 5 * $nowlevel) - 60);
		$this->assign('needjingyan',$needjingyan);

		//pk测试，搜索所有人物并创建链接
		$allpeople = D('Charview');
		$people = $allpeople->findall();
		$this->assign('people',$people);
		$this->display();
	}

	/**
	 * 属性升级操作
	 * $test = $_GET['kind']; 区分升级类型
	 * 2=力量
	 * 3=敏捷
	 * 4=智力
	 * 5=强壮
	 * 6=魅力
	 * 7=洞察
	 */
	public function levelupshuxing(){
		$test = $_GET['kind'];
		$cid = Session::get('cid');
		$charbase = D('Charbase');
		$charbase->find($cid);
		switch ($test){
			case 2:
				if($charbase->shuxingdian > 0){
						$charbase->shuxingdian -= 1;
						$charbase->liliang += 1;
						$charbase->save();
						$this->shuxingjisuan();
				}
				else{
					$this->showmessage("属性点不够！");
				}
				break;
			case 3:
				if($charbase->shuxingdian > 0){
						$charbase->shuxingdian -= 1;
						$charbase->minjie += 1;
						$charbase->save();
						$this->shuxingjisuan();
				}
				else{
					$this->showmessage("属性点不够！");
				}
				break;
			case 4:
				if($charbase->shuxingdian > 0){
						$charbase->shuxingdian -= 1;
						$charbase->zhili += 1;
						$charbase->save();
						$this->shuxingjisuan();
				}
				else{
					$this->showmessage("属性点不够！");
				}
				break;
			case 5:
				if($charbase->shuxingdian > 0){
						$charbase->shuxingdian -= 1;
						$charbase->qiangzhuang += 1;
						$charbase->save();
						$this->shuxingjisuan();
				}
				else{
					$this->showmessage("属性点不够！");
				}
				break;
			case 6:
				if($charbase->shuxingdian > 0){
						$charbase->shuxingdian -= 1;
						$charbase->meili += 1;
						$charbase->save();
						$this->shuxingjisuan();
				}
				else{
					$this->showmessage("属性点不够！");
				}
				break;
			case 7:
				if($charbase->shuxingdian > 0){
						$charbase->shuxingdian -= 1;
						$charbase->dongcha += 1;
						$charbase->save();
						$this->shuxingjisuan();
				}
				else{
					$this->showmessage("属性点不够！");
				}
				break;
		}
		$this->redirect('index');
	}

	/**
	 * 等级提升
	 *
	 */
	public function levelup(){
		$cid = Session::get('cid');
		$charbase = D('Charbase');
		$charbase->find($cid);
		$nowlevel = $charbase->level;
		$nextlevel = $nowlevel + 1;
		$needjingyan = 60*($nextlevel * $nextlevel * $nextlevel + 5 * $nextlevel) - 60 - (60*($nowlevel * $nowlevel * $nowlevel + 5 * $nowlevel) - 60);
		if ($nowlevel >= 0 && $nowlevel < 20){
			$addshuxingdian = 3;
		}
		elseif ($nowlevel >= 20 && $nowlevel < 50){
			$addshuxingdian = 4;
		}
		else {
			$addshuxingdian = 5;
		}
		if($charbase->jingyan > $needjingyan){
			$charbase->jingyan -= $needjingyan;
			$charbase->level += 1;
			$charbase->shuxingdian += $addshuxingdian;
			$charbase->save();
			$this->shuxingjisuan();
			$this->redirect('index');
		}
		else {
			$this->showmessage('经验值不够');
		}
	}

	/**
	 * 人物HP，MP上限计算
	 *
	 */
	public function shuxingjisuan(){
		$cid = Session::get('cid');
		$charbase = D('Charbase');
		$charbase->find($cid);
		$charplus = D('Charplus');
		$charplus->find($cid);
		$charplus->wugong = $charbase->liliang*5;
		$charplus->mogong = $charbase->zhili*5;
		$charplus->wufang = $charbase->lixiang*3+$charbase->qiangzhuang*2;
		$charplus->mofang = $charbase->zhili*3+$charbase->qiangzhuang*2;
		$charplus->mingzhong = $charbase->minjie+30;
		$charplus->shanbi = $charbase->minjie+10;
		$charplus->huixin = 3;
		$charplus->gongsu = $charbase->minjie+100;
		$charplus->save();
		$charbase->hp = floor($charbase->qiangzhuang*($charbase->qiangzhuang+15)/10+$charbase->level*$charbase->level/2.5+98);
		$charbase->mp = floor($charbase->zhili*($charbase->zhili+15)/15+$charbase->level*$charbase->level/3+99);
		$charbase->save();
		$this->redirect('index');
	}

	/**
	 * 手动取得活力，每分钟1点
	 *
	 */
	public function gethuoli(){
		$cid = Session::get('cid');
		$charbase = D('Charbase');
		$charbase->find($cid);
		if((time()-$charbase->lastlogin)>60){
			$charbase->huoli = $charbase->huoli + floor((time()-$charbase->lastlogin)/60);
			$charbase->lastlogin = time() - (time()-$charbase->lastlogin)%60;
			$charbase->save();
		}
		$this->redirect('index');
	}

	public function bumanxue(){
		$cid = Session::get('cid');
		$charbase = D('Charbase');
		$charbase->find($cid);
		$charbase->nowhp = $charbase->hp;
		$charbase->nowmp = $charbase->mp;
		$charbase->save();
		$this->redirect('index');
	}
}
?>